/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gob.inamhi.gui;

import gob.inamhi.entidades.Cfges;
import gob.inamhi.entidades.Copa;
import gob.inamhi.entidades.Esta;
import gob.inamhi.entidades.Prpm;
import inamhi.gob.dp.HiloTarea;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.Query;
import javax.persistence.RollbackException;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import static org.quartz.JobBuilder.newJob;

/**
 *
 * @author dhinojosa
 */
public class NuevoProceso extends javax.swing.JFrame {

//    private EntityManager entityManager;
//    private Query query;
//    private BindingGroup bindingGroup = new BindingGroup();
    private Procesador procesador;
    private Scheduler sched;

    /**
     * Creates new form NuevoProceso
     */
    public NuevoProceso(Procesador procesador, Scheduler sched) {
        this.procesador = procesador;
        this.sched = sched;
        initComponents();
    }

    public Procesador getProcesador() {
        return procesador;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        bandahmPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("bandahmPU").createEntityManager();
        copaQuery = java.beans.Beans.isDesignTime() ? null : bandahmPUEntityManager.createQuery("SELECT c FROM Copa c ORDER BY c.parmId.parmdesc");
        copaList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : copaQuery.getResultList();
        estaQuery = java.beans.Beans.isDesignTime() ? null : bandahmPUEntityManager.createQuery("SELECT e FROM Esta e ORDER BY e.estanomb");
        estaList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : estaQuery.getResultList();
        copaIdLabel = new javax.swing.JLabel();
        cfgesIdLabel = new javax.swing.JLabel();
        coparesuLabel = new javax.swing.JLabel();
        estaIdLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        cmbEstaciones = new javax.swing.JComboBox();
        cmbCopaIn = new javax.swing.JComboBox();
        cmbCfges = new javax.swing.JComboBox();
        cmbCopaOut = new javax.swing.JComboBox();
        jSeparator1 = new javax.swing.JSeparator();
        jPanel1 = new javax.swing.JPanel();
        prpminteField = new javax.swing.JTextField();
        prpmdeciLabel = new javax.swing.JLabel();
        prpmdestLabel = new javax.swing.JLabel();
        prpmfnteLabel = new javax.swing.JLabel();
        prpmoperField = new javax.swing.JTextField();
        prpmhorcLabel = new javax.swing.JLabel();
        prpmdestField = new javax.swing.JTextField();
        prpminteLabel = new javax.swing.JLabel();
        prpmhorcField = new javax.swing.JTextField();
        prpmfnteField = new javax.swing.JTextField();
        chkEstado = new javax.swing.JCheckBox();
        prpmpasoField = new javax.swing.JTextField();
        prpmpasoLabel = new javax.swing.JLabel();
        prpmporcLabel = new javax.swing.JLabel();
        prpmoperLabel = new javax.swing.JLabel();
        btnGuardar = new javax.swing.JButton();
        prpmporcField = new javax.swing.JTextField();
        prpmdeciField = new javax.swing.JTextField();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Crear Nuevo Proceso");
        setAutoRequestFocus(false);
        setResizable(false);

        copaIdLabel.setText("Parámetro IN:");

        cfgesIdLabel.setText("Sensor:");

        coparesuLabel.setText("Parámetro OUT:");

        estaIdLabel.setText("Estación:");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("CREAR UN NUEVO PROCESO");

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, estaList, cmbEstaciones);
        bindingGroup.addBinding(jComboBoxBinding);

        cmbEstaciones.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbEstacionesItemStateChanged(evt);
            }
        });

        cmbCfges.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, copaList, cmbCopaOut);
        bindingGroup.addBinding(jComboBoxBinding);

        prpmdeciLabel.setText("N° Decimales:");

        prpmdestLabel.setText("Tabla destino:");

        prpmfnteLabel.setText("Tabla fuente:");

        prpmhorcLabel.setText("Diferencia hora:");

        prpminteLabel.setText("Intervalo:");

        chkEstado.setText("Estado del proceso");
        chkEstado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkEstadoActionPerformed(evt);
            }
        });

        prpmpasoLabel.setText("Paso:");

        prpmporcLabel.setText("Porcentaje error:");

        prpmoperLabel.setText("Operación:");

        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(chkEstado)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(prpmdestLabel)
                                    .addComponent(prpmfnteLabel)
                                    .addComponent(prpmporcLabel)
                                    .addComponent(prpmhorcLabel)
                                    .addComponent(prpmpasoLabel)
                                    .addComponent(prpminteLabel)
                                    .addComponent(prpmdeciLabel)
                                    .addComponent(prpmoperLabel))
                                .addGap(9, 9, 9)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(prpmoperField, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(prpmdeciField, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(prpminteField, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(prpmpasoField, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(prpmhorcField, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(prpmporcField, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(prpmfnteField, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(prpmdestField, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(btnGuardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnCancelar)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(prpmoperLabel)
                    .addComponent(prpmoperField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(prpmdeciLabel)
                    .addComponent(prpmdeciField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(prpminteLabel)
                    .addComponent(prpminteField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(prpmpasoLabel)
                    .addComponent(prpmpasoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(prpmhorcLabel)
                    .addComponent(prpmhorcField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(prpmporcLabel)
                    .addComponent(prpmporcField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(prpmfnteLabel)
                    .addComponent(prpmfnteField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(prpmdestLabel)
                    .addComponent(prpmdestField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                .addComponent(chkEstado)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnGuardar)
                    .addComponent(btnCancelar)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(7, 7, 7)
                .addComponent(jSeparator1)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cfgesIdLabel)
                            .addComponent(copaIdLabel)
                            .addComponent(estaIdLabel)
                            .addComponent(coparesuLabel))
                        .addGap(19, 19, 19)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(cmbEstaciones, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cmbCopaIn, javax.swing.GroupLayout.PREFERRED_SIZE, 449, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cmbCfges, javax.swing.GroupLayout.PREFERRED_SIZE, 455, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cmbCopaOut, javax.swing.GroupLayout.PREFERRED_SIZE, 315, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(0, 14, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cmbCfges, cmbCopaIn, cmbCopaOut, cmbEstaciones});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(7, 7, 7)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(estaIdLabel)
                    .addComponent(cmbEstaciones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(copaIdLabel)
                    .addComponent(cmbCopaIn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cfgesIdLabel)
                    .addComponent(cmbCfges, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(coparesuLabel)
                    .addComponent(cmbCopaOut, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void chkEstadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkEstadoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_chkEstadoActionPerformed

    private void cmbEstacionesItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbEstacionesItemStateChanged
        // TODO add your handling code here:
        Esta aux = (Esta) cmbEstaciones.getSelectedItem();
        Integer estaId = aux.getEstaId();
        Query cfgesQuery = bandahmPUEntityManager.createQuery("SELECT cf FROM Cfges cf WHERE cf.estaId.estaId=" + estaId + " AND cf.cfgesfinm = (SELECT MAX(c.cfgesfinm) FROM Cfges c WHERE c.estaId.estaId=" + estaId + ") ORDER BY cf.cfgesords");
        List<Cfges> cfgesList = org.jdesktop.observablecollections.ObservableCollections.observableList(cfgesQuery.getResultList());
        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, cfgesList, cmbCfges);
        jComboBoxBinding.bind();


        Query cprestaQuery = bandahmPUEntityManager.createQuery("SELECT cp.copaId FROM Cpresta cp WHERE cp.estaId.estaId=" + estaId + " ORDER BY cp.copaId.parmId.parmdesc");
        List<Copa> copaInList = org.jdesktop.observablecollections.ObservableCollections.observableList(cprestaQuery.getResultList());
        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBindingCopaIn = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, copaInList, cmbCopaIn);
        jComboBoxBindingCopaIn.bind();
    }//GEN-LAST:event_cmbEstacionesItemStateChanged

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        // TODO add your handling code here:
        Query prpmIdQuery = bandahmPUEntityManager.createQuery("SELECT MAX(p.prpmId) FROM Prpm p");
        Integer prpmId = (Integer) prpmIdQuery.getSingleResult();
        prpmId += 1;
        Prpm p = new Prpm();
        Esta esta = (Esta) cmbEstaciones.getSelectedItem();
        Copa copaIn = (Copa) cmbCopaIn.getSelectedItem();
        Copa copaOut = (Copa) cmbCopaOut.getSelectedItem();
        Cfges cfges = (Cfges) cmbCfges.getSelectedItem();
        String operacion = prpmoperField.getText();
        Integer decimales = Integer.parseInt(prpmdeciField.getText());
        String intervalo = prpminteField.getText();
        String paso = prpmpasoField.getText();
        String horc = prpmhorcField.getText();
        Integer porcentaje = Integer.parseInt(prpmporcField.getText());
        String tablaFuente = prpmfnteField.getText();
        String tablaDestino = prpmdestField.getText();
        Boolean estado = chkEstado.isSelected();
        p.setPrpmId(prpmId);
        p.setPrpmdeci(decimales);
        p.setPrpmdest(tablaDestino);
        p.setPrpmesta(estado);
        p.setPrpmfnte(tablaFuente);
        p.setPrpmhorc(horc);
        p.setPrpminte(intervalo);
        p.setPrpmoper(operacion);
        p.setPrpmpaso(paso);
        p.setPrpmporc(porcentaje);
        p.setEstaId(esta);
        p.setCopaId(copaIn);
        p.setCoparesu(copaOut);
        p.setCfgesId(cfges);



        //  boton guardar
        try {
            bandahmPUEntityManager.getTransaction().begin();
            bandahmPUEntityManager.persist(p);
            bandahmPUEntityManager.getTransaction().commit();

            //AGENDAR NUEVO TRABAJO CREADO AL SCHEDULER
            JobDetail demoJob = newJob(HiloTarea.class).build();
            demoJob.getJobDataMap().put("copa__id", copaIn.getCopaId());  //Código del parámetro a procesar
            demoJob.getJobDataMap().put("coparesu", copaOut.getCopaId());  //Código del parámetro resultante
            demoJob.getJobDataMap().put("prpmoper", operacion);  //Tipo de operación matemática a realizarse sobre los datos
            demoJob.getJobDataMap().put("prpmdeci", decimales);  //Intervalo de calculo del dato
            demoJob.getJobDataMap().put("prpmhorc", horc);  //Intervalo de calculo del dato
            demoJob.getJobDataMap().put("prpmpaso", paso);  //Intervalo de calculo del dato
            demoJob.getJobDataMap().put("prpmfnte", tablaFuente);  //Intervalo de calculo del dato
            demoJob.getJobDataMap().put("prpmdest", tablaDestino);  //Control de estado de la tarea
            demoJob.getJobDataMap().put("prpmesta", estado);  //Control de estado de la tarea
            demoJob.getJobDataMap().put("esta__id", esta.getEstaId());  //Control de estado de la tarea
            demoJob.getJobDataMap().put("prpmporc", porcentaje);  //Control de estado de la tarea
            demoJob.getJobDataMap().put("cfges__id", cfges.getCfgesId());  //Control de estado de la tarea

            Trigger trigger = TriggerBuilder
                    .newTrigger()
                    .withIdentity("Proceso" + prpmId, "group").startNow()
                    .withSchedule(
                    CronScheduleBuilder.cronSchedule(intervalo).withMisfireHandlingInstructionFireAndProceed()) //Configuración formato cron para determinar el tiempo de ejecución de la tarea
                    .build();

            sched.scheduleJob(demoJob, trigger);
            this.dispose();

        } catch (RollbackException rex) {
            rex.printStackTrace();
            bandahmPUEntityManager.getTransaction().begin();
            List<Prpm> merged = new ArrayList<Prpm>(procesador.getList().size());
            for (Prpm prpm : procesador.getList()) {
                merged.add(bandahmPUEntityManager.merge(prpm));
            }
            procesador.getList().clear();
            procesador.getList().addAll(merged);
        } catch (SchedulerException ex) {
            Logger.getLogger(NuevoProceso.class.getName()).log(Level.SEVERE, null, ex);
        }


        //boton refrescar
//        bandahmPUEntityManager.getTransaction().rollback();
//        bandahmPUEntityManager.getTransaction().begin();
//        java.util.Collection data = procesador.getQuery().getResultList();
//        for (Object entity : data) {
//            bandahmPUEntityManager.refresh(entity);
//        }
//        procesador.getList().clear();
//        procesador.getList().addAll(data);

    }//GEN-LAST:event_btnGuardarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NuevoProceso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NuevoProceso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NuevoProceso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NuevoProceso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new NuevoProceso().setVisible(false);
//            }
//        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager bandahmPUEntityManager;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JLabel cfgesIdLabel;
    private javax.swing.JCheckBox chkEstado;
    private javax.swing.JComboBox cmbCfges;
    private javax.swing.JComboBox cmbCopaIn;
    private javax.swing.JComboBox cmbCopaOut;
    private javax.swing.JComboBox cmbEstaciones;
    private javax.swing.JLabel copaIdLabel;
    private java.util.List<gob.inamhi.entidades.Copa> copaList;
    private javax.persistence.Query copaQuery;
    private javax.swing.JLabel coparesuLabel;
    private javax.swing.JLabel estaIdLabel;
    private java.util.List<gob.inamhi.entidades.Esta> estaList;
    private javax.persistence.Query estaQuery;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField prpmdeciField;
    private javax.swing.JLabel prpmdeciLabel;
    private javax.swing.JTextField prpmdestField;
    private javax.swing.JLabel prpmdestLabel;
    private javax.swing.JTextField prpmfnteField;
    private javax.swing.JLabel prpmfnteLabel;
    private javax.swing.JTextField prpmhorcField;
    private javax.swing.JLabel prpmhorcLabel;
    private List<Copa> list;
    private javax.swing.JTextField prpminteField;
    private javax.swing.JLabel prpminteLabel;
    private javax.swing.JTextField prpmoperField;
    private javax.swing.JLabel prpmoperLabel;
    private javax.swing.JTextField prpmpasoField;
    private javax.swing.JLabel prpmpasoLabel;
    private javax.swing.JTextField prpmporcField;
    private javax.swing.JLabel prpmporcLabel;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
